package com.baicun.wish.controller;

import cn.hutool.json.JSONObject;
import com.baicun.wish.entity.CollegeMajorAdmit;
import com.baicun.wish.entity.dto.Result;
import com.baicun.wish.service.CollegeMajorAdmitService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baicun
 * @since 2020-09-09
 */
@RestController
@RequestMapping("/college-major-admit")
public class CollegeMajorAdmitController {

    @Autowired
    private CollegeMajorAdmitService collegeMajorAdmitService;

    /**
     * create index index_getCollegeMajorAdmitByCollegeName on college_major_admit(college_name,province);
     * 根据collegeName 省份 分页查询college_major_admit 中 大学各专业在各省的录取信息
     * @param jsonObject
     * @return
     */
    @PostMapping("/getCollegeMajorAdmitByCollegeName")
    public Result getCollegeMajorAdmitByCollegeName(@RequestBody JSONObject jsonObject){
        Integer currentPage = jsonObject.getInt("currentPage");
        String collegeName = jsonObject.getStr("collegeName");
        String provinceMajorAdmit = jsonObject.getStr("provinceMajorAdmit");

        QueryWrapper<CollegeMajorAdmit> collegeMajorAdmitQueryWrapper = new QueryWrapper<CollegeMajorAdmit>().eq("college_name",collegeName);
        Page page = new Page(currentPage,10);

        if (!"全部".equals(provinceMajorAdmit)){
            collegeMajorAdmitQueryWrapper = collegeMajorAdmitQueryWrapper.eq("province",provinceMajorAdmit);
        }
        IPage<CollegeMajorAdmit> iPage = collegeMajorAdmitService.page(page,collegeMajorAdmitQueryWrapper.orderByDesc("province","year"));

        if (iPage.getRecords().isEmpty()){
            return Result.success(201,"查询列表为空",iPage);
        }else{
            return Result.success(iPage);
        }
    }

}
